import {getStore, setStore} from '@/libs/utils/store'
import {destoryToken, getTokenByUsername, refreshToken} from '@/projects/basic/api/token'
import {getUserInfo} from '@/projects/basic/api/admin/user'
import {refreshSSoToken} from '@/projects/basic/api/gwmfc-sso'
const user = {
    state: {
        userInfo: getStore({
            name: 'user_info'
        }) || {},
        userAllInfo: getStore({
            name: 'user_all_info'
        }) || {},
        permissions: getStore({
            name: 'permissions'
        }) || {},
        roles: getStore({
            name: 'roles'
        }) || [],
        menu: getStore({
            name: 'menu'
        }) || [],
        expires_in: getStore({
            name: 'expires_in'
        }) || '',
        access_token: getStore({
            name: 'access_token'
        }) || '',
        refresh_token: getStore({
            name: 'refresh_token'
        }) || '',
        locking: getStore({
            name: 'locking'
        }) || '0',
        changePassWord: getStore({
            name: 'changePassWord'
        }),
        lastAccessTime: getStore({
            name: 'lastAccessTime'
        }),
        sso_expires_in: getStore({
            name: 'sso_expires_in'
        }) || '',
        sso_access_token: getStore({
            name: 'sso_access_token'
        }) || '',
    },
    actions: {
        // 根据用户名登录
        LoginByUsername({commit}, userInfo) {
            const user = userInfo
            return new Promise((resolve, reject) => {
                getTokenByUsername(userInfo).then(response => {
                    const data = response.data
                    commit('SET_ACCESS_TOKEN', data.access_token)
                    commit('SET_REFRESH_TOKEN', data.refresh_token)
                    commit('SET_EXPIRES_IN', data.expires_in)
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        GetUserInfo({commit}) {
            return new Promise((resolve, reject) => {
                getUserInfo().then((res) => {
                    const data = res.data || {}
                    let roleList = [];
                    data.roleList.forEach(roleVo => {
                        roleList.push(roleVo.roleCode)
                    })
                    commit('SET_USER_INFO', data.sysUser)
                    commit('SET_ROLES', roleList)
                    commit('SET_PERMISSIONS', data.permissions || [])
                    commit('SET_FORCE_CHANGE_PASS', data.needChangePass || false)
                    commit("setHasLogin",true)
                    commit("SET_USER_ALL_INFO",data)
                    resolve(data)
                }).catch((err) => {
                    reject(err)
                })
            })
        },
        // 刷新token
        RefreshToken({commit, state}) {
            return new Promise((resolve, reject) => {
                refreshToken(state.refresh_token).then(response => {
                    const data = response.data
                    commit('SET_ACCESS_TOKEN', data.access_token)
                    commit('SET_REFRESH_TOKEN', data.refresh_token)
                    commit('SET_EXPIRES_IN', data.expires_in)
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        RefreshOssToken({commit, state}) {
            return new Promise((resolve, reject) => {
                refreshSSoToken(state.sso_access_token).then(response => {
                    const data = response.data
                    if(data.validate) {
                        commit('SET_SSO_ACCESS_TOKEN', data.token)
                        commit('SET_SSO_EXPIRES_IN', data.tokenExpireTime)
                    }
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 主动登出
        LogOut({commit}) {
            return new Promise((resolve, reject) => {
                destoryToken().then(() => {
                    commit('SET_MENU', [])
                    commit('SET_PERMISSIONS', [])
                    commit('SET_USER_INFO', {})
                    commit("SET_USER_ALL_INFO", {})
                    commit('SET_ACCESS_TOKEN', '')
                    commit('SET_SSO_ACCESS_TOKEN', '')
                    commit('SET_REFRESH_TOKEN', '')
                    commit('SET_EXPIRES_IN', 0)
                    commit('SET_SSO_EXPIRES_IN', 0)
                    commit('SET_ROLES', [])
                    commit('clearTagNavList', [])
                    commit('SET_LAST_ACCESS_TIME', Date.now()+1000*60)
                    resolve()
                }).catch(error => {
                    // reject(error)
                    commit('SET_MENU', [])
                    commit('SET_PERMISSIONS', [])
                    commit('SET_USER_INFO', {})
                    commit("SET_USER_ALL_INFO",{})
                    commit('SET_ACCESS_TOKEN', '')
                    commit('SET_SSO_ACCESS_TOKEN', '')
                    commit('SET_REFRESH_TOKEN', '')
                    commit('SET_EXPIRES_IN', 0)
                    commit('SET_SSO_EXPIRES_IN', 0)
                    commit('SET_ROLES', [])
                    commit('clearTagNavList', [])
                    commit('SET_LAST_ACCESS_TIME', Date.now()+1000*60)
                    resolve()
                })
            })
        }
        ,
        // 自动登出
        CLEAN_SESSION({commit}) {
            return new Promise(resolve => {
                commit('SET_MENU', [])
                commit('SET_PERMISSIONS', [])
                commit('SET_USER_INFO', {})
                commit("SET_USER_ALL_INFO", {})
                commit('SET_ACCESS_TOKEN', '')
                commit('SET_SSO_ACCESS_TOKEN', '')
                commit('SET_REFRESH_TOKEN', '')
                commit('SET_EXPIRES_IN', 0)
                commit('SET_SSO_EXPIRES_IN', 0)
                commit('SET_ROLES', [])
                commit('clearTagNavList', [])
                commit('SET_LAST_ACCESS_TIME', Date.now()+1000*60)
                resolve()
            })
        },
        UPDATE_LAST_ACCESS_TIME({commit}) {
            return new Promise(resolve => {
                commit('SET_LAST_ACCESS_TIME', Date.now())
                resolve()
            })
        }
    },
    mutations: {
        SET_ACCESS_TOKEN: (state, access_token) => {
            state.access_token = access_token
            setStore({
                name: 'access_token',
                content: state.access_token,
                type: 'session'
            });
        },
        SET_EXPIRES_IN: (state, expires_in) => {
            state.expires_in = expires_in * 1000 + (Date.now())
            setStore({
                name: 'expires_in',
                content: state.expires_in,
                type: 'session'
            })
        },
        SET_SSO_ACCESS_TOKEN: (state, access_token) => {
            state.sso_access_token = access_token
            setStore({
                name: 'sso_access_token',
                content: state.sso_access_token,
                type: 'local'
            });
        },
        SET_SSO_EXPIRES_IN: (state, expires_in) => {
            state.sso_expires_in = expires_in
            setStore({
                name: 'sso_expires_in',
                content: state.sso_expires_in,
                type: 'local'
            })
        },
        SET_REFRESH_TOKEN: (state, rfToken) => {
            state.refresh_token = rfToken
            setStore({
                name: 'refresh_token',
                content: state.refresh_token,
                type: 'session'
            })
        },
        SET_USER_INFO: (state, userInfo) => {
            state.userInfo = userInfo
            setStore({
                name: 'user_info',
                content: state.userInfo,
                type: 'session'
            })
        },
        SET_USER_ALL_INFO: (state, userAllInfo) => {
            state.userAllInfo = userAllInfo
            setStore({
                name: 'user_all_info',
                content: state.userAllInfo,
                type: 'session'
            })
        },
        SET_MENU: (state, menu) => {
            state.menu = menu
            setStore({
                name: 'menu',
                content: state.menu,
                type: 'session'
            })
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
            setStore({
                name: 'roles',
                content: state.roles,
                type: 'session'
            })
        },
        SET_PERMISSIONS: (state, permissions) => {
            const list = {}
            for (let i = 0; i < permissions.length; i++) {
                list[permissions[i]] = true
            }
            state.permissions = list
            setStore({
                name: 'permissions',
                content: state.permissions,
                type: 'session'
            })
        },
        SET_FORCE_CHANGE_PASS: (state, change) => {
            state.changePassWord = change;
            setStore({
                name: 'changePassWord',
                content: state.changePassWord,
                type: 'session'
            })
        },
        SET_LAST_ACCESS_TIME:(state,lastAccessTime)=>{
            state.lastAccessTime = lastAccessTime;
            setStore({
                name: 'lastAccessTime',
                content: state.lastAccessTime,
                type: 'session'
            })
        }
    }
}
export default user
